import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

// 地理坐标模型
class GeoCoordinates {
  final double lat;
  final double lng;

  GeoCoordinates({required this.lat, required this.lng});

  factory GeoCoordinates.fromJson(Map<String, dynamic> json) {
    return GeoCoordinates(
      lat: (json['lat'] ?? 0.0).toDouble(),
      lng: (json['lng'] ?? 0.0).toDouble(),
    );
  }

  Map<String, dynamic> toJson() {
    return {'lat': lat, 'lng': lng};
  }

  LatLng toLatLng() {
    return LatLng(lat, lng);
  }
}

// 价格范围模型
class PriceRange {
  final double min;
  final double max;
  final String currency;

  PriceRange({required this.min, required this.max, required this.currency});

  factory PriceRange.fromJson(Map<String, dynamic> json) {
    return PriceRange(
      min: (json['min'] ?? 0.0).toDouble(),
      max: (json['max'] ?? 1000.0).toDouble(),
      currency: json['currency'] ?? 'CNY',
    );
  }

  Map<String, dynamic> toJson() {
    return {'min': min, 'max': max, 'currency': currency};
  }

  String get displayRange => '¥${min.toInt()}-${max.toInt()}';
}

// 交通信息模型
class TransportationInfo {
  final String? airport;
  final String? publicTransport;
  final String? driveTime;

  TransportationInfo({this.airport, this.publicTransport, this.driveTime});

  factory TransportationInfo.fromJson(Map<String, dynamic> json) {
    return TransportationInfo(
      airport: json['airport'],
      publicTransport: json['publicTransport'],
      driveTime: json['driveTime'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'airport': airport,
      'publicTransport': publicTransport,
      'driveTime': driveTime,
    };
  }
}

class AttractionInfo {
  final String id;
  final String name;
  final String nameEn;
  final String description;
  final String image;
  final double rating;
  final int reviewCount;
  final PriceRange? priceRange;
  final GeoCoordinates? coordinates;

  AttractionInfo({
    required this.id,
    required this.name,
    required this.nameEn,
    required this.description,
    required this.image,
    required this.rating,
    required this.reviewCount,
    this.priceRange,
    this.coordinates,
  });

  factory AttractionInfo.fromJson(Map<String, dynamic> json) {
    return AttractionInfo(
      id: json['_id'] ?? json['id'] ?? '',
      name: json['name'] ?? '',
      nameEn: json['nameEn'] ?? '',
      description: json['description'] ?? '',
      image: json['image'] ?? '',
      rating: (json['rating'] ?? 0.0).toDouble(),
      reviewCount: json['reviewCount'] ?? 0,
      priceRange:
          json['priceRange'] != null
              ? PriceRange.fromJson(json['priceRange'])
              : null,
      coordinates:
          json['coordinates'] != null
              ? GeoCoordinates.fromJson(json['coordinates'])
              : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'nameEn': nameEn,
      'description': description,
      'image': image,
      'rating': rating,
      'reviewCount': reviewCount,
      'priceRange': priceRange?.toJson(),
      'coordinates': coordinates?.toJson(),
    };
  }
}

class HotelInfo {
  final String id;
  final String name;
  final String priceRange;
  final double rating;
  final String image;

  HotelInfo({
    required this.id,
    required this.name,
    required this.priceRange,
    required this.rating,
    required this.image,
  });

  factory HotelInfo.fromJson(Map<String, dynamic> json) {
    return HotelInfo(
      id: json['_id'] ?? json['id'] ?? '',
      name: json['name'] ?? '',
      priceRange: json['price_range'] ?? json['priceRange'] ?? '',
      rating: (json['rating'] ?? 0.0).toDouble(),
      image: json['image'] ?? '',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'price_range': priceRange,
      'rating': rating,
      'image': image,
    };
  }
}

// 目的地详情模型
class DestinationDetail {
  final String id;
  final String name;
  final String nameEn;
  final String description;
  final String? shortDescription;
  final String country;
  final String? region;
  final String? address;
  final String image;
  final List<String> imageGallery;
  final GeoCoordinates coordinates;
  final double rating;
  final int reviewCount;
  final int popularLevel;
  final bool isPopular;
  final String category;
  final List<String> tags;
  final PriceRange priceRange;
  final String bestSeason;
  final String visitDuration;
  final TransportationInfo? transportation;
  final String? language;
  final String? timezone;
  final String? climate;
  final String? currency;
  final String status;
  final List<AttractionInfo> attractions;
  final List<HotelInfo> hotels;
  final List<DestinationSummary> nearbyDestinations;
  final DateTime createdAt;
  final DateTime updatedAt;

  DestinationDetail({
    required this.id,
    required this.name,
    required this.nameEn,
    required this.description,
    this.shortDescription,
    required this.country,
    this.region,
    this.address,
    required this.image,
    required this.imageGallery,
    required this.coordinates,
    required this.rating,
    required this.reviewCount,
    required this.popularLevel,
    required this.isPopular,
    required this.category,
    required this.tags,
    required this.priceRange,
    required this.bestSeason,
    required this.visitDuration,
    this.transportation,
    this.language,
    this.timezone,
    this.climate,
    this.currency,
    required this.status,
    required this.attractions,
    required this.hotels,
    required this.nearbyDestinations,
    required this.createdAt,
    required this.updatedAt,
  });

  factory DestinationDetail.fromJson(Map<String, dynamic> json) {
    return DestinationDetail(
      id: json['_id'] ?? json['id'] ?? '',
      name: json['name'] ?? '',
      nameEn: json['nameEn'] ?? '',
      description: json['description'] ?? '',
      shortDescription: json['shortDescription'],
      country: json['country'] ?? '',
      region: json['region'],
      address: json['address'],
      image: json['image'] ?? '',
      imageGallery: List<String>.from(json['imageGallery'] ?? []),
      coordinates: GeoCoordinates.fromJson(json['coordinates'] ?? {}),
      rating: (json['rating'] ?? 0.0).toDouble(),
      reviewCount: json['reviewCount'] ?? 0,
      popularLevel: json['popularLevel'] ?? 3,
      isPopular: json['isPopular'] ?? false,
      category: json['category'] ?? '',
      tags: List<String>.from(json['tags'] ?? []),
      priceRange: PriceRange.fromJson(json['priceRange'] ?? {}),
      bestSeason: json['bestSeason'] ?? '全年',
      visitDuration: json['visitDuration'] ?? '1天',
      transportation:
          json['transportation'] != null
              ? TransportationInfo.fromJson(json['transportation'])
              : null,
      language: json['language'],
      timezone: json['timezone'],
      climate: json['climate'],
      currency: json['currency'],
      status: json['status'] ?? 'active',
      attractions:
          (json['attractions'] as List<dynamic>?)
              ?.map((e) => AttractionInfo.fromJson(e))
              .toList() ??
          [],
      hotels:
          (json['hotels'] as List<dynamic>?)
              ?.map((e) => HotelInfo.fromJson(e))
              .toList() ??
          [],
      nearbyDestinations:
          (json['nearbyDestinations'] as List<dynamic>?)
              ?.map((e) => DestinationSummary.fromJson(e))
              .toList() ??
          [],
      createdAt:
          json['createdAt'] != null
              ? DateTime.parse(json['createdAt'])
              : DateTime.now(),
      updatedAt:
          json['updatedAt'] != null
              ? DateTime.parse(json['updatedAt'])
              : DateTime.now(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      '_id': id,
      'name': name,
      'nameEn': nameEn,
      'description': description,
      'shortDescription': shortDescription,
      'country': country,
      'region': region,
      'address': address,
      'image': image,
      'imageGallery': imageGallery,
      'coordinates': coordinates.toJson(),
      'rating': rating,
      'reviewCount': reviewCount,
      'popularLevel': popularLevel,
      'isPopular': isPopular,
      'category': category,
      'tags': tags,
      'priceRange': priceRange.toJson(),
      'bestSeason': bestSeason,
      'visitDuration': visitDuration,
      'transportation': transportation?.toJson(),
      'language': language,
      'timezone': timezone,
      'climate': climate,
      'currency': currency,
      'status': status,
      'attractions': attractions.map((e) => e.toJson()).toList(),
      'hotels': hotels.map((e) => e.toJson()).toList(),
      'nearbyDestinations': nearbyDestinations.map((e) => e.toJson()).toList(),
      'createdAt': createdAt.toIso8601String(),
      'updatedAt': updatedAt.toIso8601String(),
    };
  }
}

// 目的地简要信息模型（用于列表和附近目的地）
class DestinationSummary {
  final String id;
  final String name;
  final String nameEn;
  final String image;
  final double rating;
  final GeoCoordinates? coordinates;
  final String country;

  DestinationSummary({
    required this.id,
    required this.name,
    required this.nameEn,
    required this.image,
    required this.rating,
    this.coordinates,
    required this.country,
  });

  factory DestinationSummary.fromJson(Map<String, dynamic> json) {
    return DestinationSummary(
      id: json['_id'] ?? json['id'] ?? '',
      name: json['name'] ?? '',
      nameEn: json['nameEn'] ?? '',
      image: json['image'] ?? '',
      rating: (json['rating'] ?? 0.0).toDouble(),
      coordinates:
          json['coordinates'] != null
              ? GeoCoordinates.fromJson(json['coordinates'])
              : null,
      country: json['country'] ?? '',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'nameEn': nameEn,
      'image': image,
      'rating': rating,
      'coordinates': coordinates?.toJson(),
      'country': country,
    };
  }
}

class DestinationModel {
  final String id;
  final String name;
  final String nameEn;
  final String description;
  final String country;
  final String region;
  final double latitude;
  final double longitude;
  final List<String> images;
  final String coverImage;
  final double rating;
  final int reviewCount;
  final List<String> tags;
  final Map<String, dynamic> climate;
  final String bestTravelTime;
  final List<String> languages;
  final String currency;
  final int timeZone;
  final List<AttractionInfo> attractions; // 景点列表
  final List<HotelInfo> hotels; // 酒店推荐
  final String geoTag; // 地理标签
  final DateTime createdAt;
  final DateTime updatedAt;

  DestinationModel({
    required this.id,
    required this.name,
    required this.nameEn,
    required this.description,
    required this.country,
    required this.region,
    required this.latitude,
    required this.longitude,
    required this.images,
    required this.coverImage,
    required this.rating,
    required this.reviewCount,
    required this.tags,
    required this.climate,
    required this.bestTravelTime,
    required this.languages,
    required this.currency,
    required this.timeZone,
    this.attractions = const [],
    this.hotels = const [],
    required this.geoTag,
    required this.createdAt,
    required this.updatedAt,
  });

  factory DestinationModel.fromJson(Map<String, dynamic> json) {
    return DestinationModel(
      id: json['_id'] ?? json['id'] ?? '',
      name: json['name'] ?? '',
      nameEn: json['nameEn'] ?? '',
      description: json['description'] ?? '',
      country: json['country'] ?? '',
      region: json['region'] ?? '',
      latitude: (json['latitude'] ?? 0.0).toDouble(),
      longitude: (json['longitude'] ?? 0.0).toDouble(),
      images: List<String>.from(json['images'] ?? []),
      coverImage: json['coverImage'] ?? '',
      rating: (json['rating'] ?? 0.0).toDouble(),
      reviewCount: json['reviewCount'] ?? 0,
      tags: List<String>.from(json['tags'] ?? []),
      climate: Map<String, dynamic>.from(json['climate'] ?? {}),
      bestTravelTime: json['bestTravelTime'] ?? '',
      languages: List<String>.from(json['languages'] ?? []),
      currency: json['currency'] ?? '',
      timeZone: json['timeZone'] ?? 0,
      attractions:
          (json['attractions'] as List<dynamic>?)
              ?.map((e) => AttractionInfo.fromJson(e))
              .toList() ??
          [],
      hotels:
          (json['hotels'] as List<dynamic>?)
              ?.map((e) => HotelInfo.fromJson(e))
              .toList() ??
          [],
      geoTag: json['geo_tag'] ?? '',
      createdAt:
          json['createdAt'] != null
              ? DateTime.parse(json['createdAt'])
              : DateTime.now(),
      updatedAt:
          json['updatedAt'] != null
              ? DateTime.parse(json['updatedAt'])
              : DateTime.now(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      '_id': id,
      'name': name,
      'nameEn': nameEn,
      'description': description,
      'country': country,
      'region': region,
      'latitude': latitude,
      'longitude': longitude,
      'images': images,
      'coverImage': coverImage,
      'rating': rating,
      'reviewCount': reviewCount,
      'tags': tags,
      'climate': climate,
      'bestTravelTime': bestTravelTime,
      'languages': languages,
      'currency': currency,
      'timeZone': timeZone,
      'attractions': attractions.map((e) => e.toJson()).toList(),
      'hotels': hotels.map((e) => e.toJson()).toList(),
      'geo_tag': geoTag,
      'createdAt': createdAt.toIso8601String(),
      'updatedAt': updatedAt.toIso8601String(),
    };
  }
}

class Destination {
  final String id;
  final String name;
  final String description;
  final String imageUrl;
  final String category;
  final String location;
  final double rating;
  final int reviewCount;
  final double price;
  final List<String> tags;
  final bool isPopular;
  final DateTime createdAt;

  Destination({
    required this.id,
    required this.name,
    required this.description,
    required this.imageUrl,
    required this.category,
    required this.location,
    required this.rating,
    required this.reviewCount,
    required this.price,
    required this.tags,
    required this.isPopular,
    required this.createdAt,
  });

  factory Destination.fromJson(Map<String, dynamic> json) {
    return Destination(
      id: json['id'] ?? json['_id'] ?? '',
      name: json['name'] ?? '',
      description: json['description'] ?? '',
      imageUrl: json['imageUrl'] ?? json['image'] ?? '',
      category: json['category'] ?? '',
      location: json['location'] ?? '',
      rating: (json['rating'] ?? 0.0).toDouble(),
      reviewCount: json['reviewCount'] ?? 0,
      price: (json['price'] ?? 0.0).toDouble(),
      tags: List<String>.from(json['tags'] ?? []),
      isPopular: json['isPopular'] ?? false,
      createdAt:
          json['createdAt'] != null
              ? DateTime.parse(json['createdAt'])
              : DateTime.now(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'description': description,
      'imageUrl': imageUrl,
      'category': category,
      'location': location,
      'rating': rating,
      'reviewCount': reviewCount,
      'price': price,
      'tags': tags,
      'isPopular': isPopular,
      'createdAt': createdAt.toIso8601String(),
    };
  }

  Destination copyWith({
    String? id,
    String? name,
    String? description,
    String? imageUrl,
    String? category,
    String? location,
    double? rating,
    int? reviewCount,
    double? price,
    List<String>? tags,
    bool? isPopular,
    DateTime? createdAt,
  }) {
    return Destination(
      id: id ?? this.id,
      name: name ?? this.name,
      description: description ?? this.description,
      imageUrl: imageUrl ?? this.imageUrl,
      category: category ?? this.category,
      location: location ?? this.location,
      rating: rating ?? this.rating,
      reviewCount: reviewCount ?? this.reviewCount,
      price: price ?? this.price,
      tags: tags ?? this.tags,
      isPopular: isPopular ?? this.isPopular,
      createdAt: createdAt ?? this.createdAt,
    );
  }
}

// 目的地分页响应模型
class DestinationPageResponse {
  final List<Destination> destinations;
  final int currentPage;
  final int totalPages;
  final int totalCount;
  final int pageSize;

  DestinationPageResponse({
    required this.destinations,
    required this.currentPage,
    required this.totalPages,
    required this.totalCount,
    required this.pageSize,
  });

  factory DestinationPageResponse.fromJson(Map<String, dynamic> json) {
    return DestinationPageResponse(
      destinations:
          (json['destinations'] as List<dynamic>?)
              ?.map((item) => Destination.fromJson(item))
              .toList() ??
          [],
      currentPage: json['currentPage'] ?? 1,
      totalPages: json['totalPages'] ?? 1,
      totalCount: json['totalCount'] ?? 0,
      pageSize: json['pageSize'] ?? 10,
    );
  }
}

// 排序选项枚举
enum DestinationSortOption {
  popular('popular', '热门度', Icons.trending_up),
  name('name', '名称', Icons.sort_by_alpha),
  rating('rating', '评分', Icons.star),
  price('price', '价格', Icons.attach_money),
  newest('newest', '最新', Icons.access_time);

  const DestinationSortOption(this.value, this.label, this.icon);

  final String value;
  final String label;
  final IconData icon;
}

// 目的地类别枚举
enum DestinationCategory {
  all('all', '全部', Icons.explore),
  city('city', '城市', Icons.location_city),
  nature('nature', '自然风光', Icons.nature),
  culture('culture', '文化古迹', Icons.account_balance),
  beach('beach', '海滩度假', Icons.beach_access),
  mountain('mountain', '山地探险', Icons.terrain),
  food('food', '美食之旅', Icons.restaurant);

  const DestinationCategory(this.value, this.label, this.icon);

  final String value;
  final String label;
  final IconData icon;
}
